package com.systop.ltbook.favorite.webapp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.systop.cms.utils.PageUtil;
import com.systop.common.modules.security.user.LoginUserService;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.dao.support.Page;
import com.systop.core.webapp.struts2.action.ExtJsCrudAction;
import com.systop.ltbook.BookConstants;
import com.systop.ltbook.book.model.Book;
import com.systop.ltbook.book.subject.service.SubjectManager;
import com.systop.ltbook.favorite.model.Favorite;
import com.systop.ltbook.favorite.service.FavoriteManager;

/**
 * 我的收藏管理Action
 * @author DU
 *
 */
@SuppressWarnings({"serial","unchecked"})
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class FavoriteAction extends ExtJsCrudAction<Favorite, FavoriteManager> {

	/** 图书ID */
	private Integer bookId;
	
	/** 图书名称 */
	private String bookName;
	
	/** 所属学科*/
	private Integer bookSubjectId;
	
	/** 年级 */
	private String bookGrade;
	
	/**
	 * json返回结果
	 */
	private Map<String, String> addResult;
	
	/**
	 * 登陆用户信息管理
	 */
	@Autowired
	private LoginUserService loginUserService;
	
	@Autowired
	private SubjectManager subjectManager;
	
	/**
	 * 我的收藏列表
	 */
	@Override
	public String index() {
		StringBuffer hql = new StringBuffer();
		hql.append("from Favorite fv where 1=1");
		List args = new ArrayList();
		User loginUser = loginUserService.getLoginUser(getRequest());
		if (loginUser != null) {
			//登录用户的收藏
			hql.append(" and fv.owner.id = ?");
			args.add(loginUser.getId());
			//按图书名称查询
			if (StringUtils.isNotBlank(bookName)) {
				hql.append(" and fv.book.name like ?");
				args.add(MatchMode.ANYWHERE.toMatchString(bookName));
			}
			//按年级查询
			if (StringUtils.isNotBlank(bookGrade)) {
				//小学、初中、高中、考试
				if (bookGrade.equals(BookConstants.GRADE_PRIMARY) || 
						bookGrade.equals(BookConstants.GRADE_JUNIOR) || 
						bookGrade.equals(BookConstants.GRADE_SENIOR) ||
						bookGrade.equals(BookConstants.GRADE_EXAM)) {
					hql.append(" and fv.book.grade like ?");
					args.add(MatchMode.ANYWHERE.toMatchString(bookGrade));
				}else {
					hql.append(" and fv.book.grade = ?");
					args.add(bookGrade);
				}
			}
			
			//按学科查询
			if (bookSubjectId != null) {
				hql.append(" and fv.book.subject.id = ?");
	      args.add(bookSubjectId);
			}
			Page pg = PageUtil.getPage(getPageNo(), getPageSize());
			page = getManager().pageQuery(pg, hql.toString(), args.toArray());
			restorePageData(page);
		}
		
		return INDEX;
	}

	/**
	 * 添加收藏
	 */
	public String addFavorite() {
		logger.debug("要收藏的图书ID:" + bookId);
		Favorite favorite = new Favorite();
		User loginUser = loginUserService.getLoginUser(getRequest());
		addResult = Collections.synchronizedMap(new HashMap<String, String>());
		if (bookId != null) {
			if (loginUser != null) {
				Book book = getManager().getDao().get(Book.class, bookId);
				favorite.setBook(book);
				favorite.setCreateDate(new Date());
				favorite.setOwner(loginUser);
				if (!getManager().isAddFavorite(loginUser, book)) {
					getManager().save(favorite);
					addResult.put("result", "success");
				} else {
					addResult.put("result", "added");
				}
			} else {
				addResult.put("result", "error");
			}
		}
		
		return "jsonRst";
	}
	
	/**
	 * 取得所有的学科列表
	 * @return
	 */
	public List<Map> getSubjectList() {
		List list = Collections.EMPTY_LIST;
    list = subjectManager.getAllSubjects();
    return list;
	}
	
	/**
	 * 图书类型
	 */
	public Map getBookTypeMap() {
		return BookConstants.BOOK_TYPE_MAP;
	}
	
	/**
	 * 取得年级_下拉框
	 */
	public Map getGradeMap() {
		 return BookConstants.GRADE_MAP;
	}
	public Map getGradeNameMap() {
		return BookConstants.GRADE_NAME_MAP;
	}
	
	public Integer getBookId() {
  	return bookId;
  }

	public void setBookId(Integer bookId) {
  	this.bookId = bookId;
  }

	public String getBookName() {
  	return bookName;
  }

	public void setBookName(String bookName) {
  	this.bookName = bookName;
  }

	public Integer getBookSubjectId() {
  	return bookSubjectId;
  }

	public void setBookSubjectId(Integer bookSubjectId) {
  	this.bookSubjectId = bookSubjectId;
  }

	public String getBookGrade() {
  	return bookGrade;
  }

	public void setBookGrade(String bookGrade) {
  	this.bookGrade = bookGrade;
  }

	public Map<String, String> getAddResult() {
  	return addResult;
  }

	public void setAddResult(Map<String, String> addResult) {
  	this.addResult = addResult;
  }
}
